package dongze.com.ckdj.fragment;

import android.support.v7.app.AlertDialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import dongze.com.ckdj.R;
import dongze.com.ckdj.adapter.NamePlateWorkLoadListAdapter;
import dongze.com.ckdj.dagger.component.AppComponent;
import dongze.com.ckdj.data.entity.response.GetNamePlateWorkLoadResponse;
import dongze.com.ckdj.databinding.FragmentNamePlateWorkLoadBinding;
import dongze.com.ckdj.databinding.model.NamePlateWorkLoadFragmentCountModel;
import dongze.com.ckdj.map.MapUtil;
import io.reactivex.observers.DisposableObserver;

/**
 * Created by fanqwz on 2017/9/4.
 */

public class NamePlateWorkLoadFragment extends ProcessableFragment {
    String brand;
    String voyage;
    NamePlateWorkLoadFragmentCountModel countModel = new NamePlateWorkLoadFragmentCountModel();

    @BindView(R.id.viewpager)
    ViewPager viewPager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            brand = getArguments().getString("brandcode");
            voyage = getArguments().getString("voyage");
        }

        if (brand != null && voyage != null) {
            loadWorkLoad();
        } else {
            getFragmentManager().beginTransaction()
                    .remove(this)
                    .commitAllowingStateLoss();
            Toast.makeText(getActivity(), "brand null", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    protected View createContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FragmentNamePlateWorkLoadBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_name_plate_work_load, container, false);
        binding.setCount(countModel);
        ButterKnife.bind(this, binding.getRoot());
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin));
        return binding.getRoot();
    }

    private void loadWorkLoad() {
        mapUtil.getNamePlateWorkLoad(brand, voyage, new DisposableObserver<GetNamePlateWorkLoadResponse>() {
            @Override
            protected void onStart() {
                super.onStart();
                doProcessing();
            }

            @Override
            public void onNext(GetNamePlateWorkLoadResponse value) {
                countModel.finished.set(String.valueOf(value.getInImgNum()));
                countModel.unfinished.set(String.valueOf(value.getNoImgNum()));
                viewPagerAdapter.setData(value.getNoImg(), value.getInImg());
            }

            @Override
            public void onError(Throwable e) {
                finishProcessing();
                new AlertDialog.Builder(getActivity())
                        .setMessage(e.getMessage())
                        .create()
                        .show();
            }

            @Override
            public void onComplete() {
                finishProcessing();
            }
        });
    }

    private MyPagerAdapter viewPagerAdapter = new MyPagerAdapter();
    private class MyPagerAdapter extends PagerAdapter{
        NamePlateWorkLoadListAdapter unfinished = new NamePlateWorkLoadListAdapter();
        NamePlateWorkLoadListAdapter finished = new NamePlateWorkLoadListAdapter();

        public void setData(List<GetNamePlateWorkLoadResponse.ImageInfo> unfinished, List<GetNamePlateWorkLoadResponse.ImageInfo> finished) {
            this.unfinished.setData(unfinished);
            this.finished.setData(finished);
            this.unfinished.notifyDataSetChanged();
            this.finished.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            CardView cardView = new CardView(container.getContext());
            ListView listView = new ListView(container.getContext());
            cardView.addView(listView);
            container.addView(cardView);
            listView.setAdapter(position == 0 ? unfinished : finished);
            return cardView;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0 ? "未完成" : "已完成";
        }

        @Override
        public float getPageWidth(int position) {
            if (1 == position) {
                return 0.9f;
            } else {
                return 0.9f;
            }
        }
    };
}
